#UI的TCP模块/必备
#添加接收数据子函数供ui调用
import socket
class TCPClient:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.tcp_socket.connect((self.host, self.port))

    def send_data(self, data):
        try:
            # 创建一个 TCP socket
            self.tcp_socket.sendall(data.encode())
        except Exception as e:
            print("发送数据时出错:", e)
    #接收数据       
    def recv_data(self, buffer_size=1024):
        try:
            # 接收数据
            data = self.tcp_socket.recv(buffer_size).decode()
            return data
        except Exception as e:
            print("接收数据时出错:", e)
    def close_connection(self):
        self.tcp_socket.close()

